<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        canvas{
            border:1px solid #ccc ;
        }
    </style>
</head>
<body>
    <script>
         (()=>{
            const canvas = document.createElement('canvas');
            canvas.width = 400 ;
            canvas.height = 400 ;
            document.body.append(canvas);

            const ctx = canvas.getContext('2d');

            ctx.beginPath();
            ctx.moveTo(50,100);
            ctx.lineTo(50,200);
            ctx.moveTo(250,100);
            ctx.lineTo(250,200);
            ctx.stroke();


            ctx.lineWidth = 10 ;
            ctx.strokeStyle = '#00f';
            ctx.setLineDash([200]);
            ctx.lineDashOffset = 200 ;

            function move(){
                ctx.clearRect(50,145,200,10) ;
                ctx.beginPath();
                ctx.lineDashOffset -= 1 ;
                ctx.moveTo(50,150);
                ctx.lineTo(250,150);
                ctx.stroke();
                
                if(ctx.lineDashOffset == -200){
                    ctx.lineDashOffset = 200 ;
                }
                requestAnimationFrame(move);
            }
            requestAnimationFrame(move);
        })();    
        
        
        (()=>{
            const canvas = document.createElement('canvas');
            canvas.width = 400 ;
            canvas.height = 400 ;
            document.body.append(canvas);

            const ctx = canvas.getContext('2d');

            ctx.lineWidth = 5 ;
            ctx.strokeStyle = '#00f';
            ctx.setLineDash([10]);

            function move(){
                ctx.clearRect(0,0,400,400);
                ctx.beginPath();
                ctx.lineDashOffset += 1 ;
                ctx.rect(100,100,200,200);
                ctx.stroke();
                
                requestAnimationFrame(move);
            }
            requestAnimationFrame(move);

        })();

    </script>
</body>
</html>